package cn.hejiang.org.controller;


import cn.hejiang.basic.exception.BusinessException;
import cn.hejiang.basic.util.AjaxResult;
import cn.hejiang.basic.util.PageList;
import cn.hejiang.org.domain.Shop;
import cn.hejiang.org.query.ShopQuery;
import cn.hejiang.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shop")
@Api(tags = "店铺接口", description = "店铺接口详细描述")
public class ShopController {

    @Autowired
    private IShopService shopService;

    // 新增店铺或修改店铺
    @PutMapping
    @ApiOperation(value = "店铺添加或修改", notes = "带Id为添加，没有就是新增")
    public AjaxResult addOrUpdate(@RequestBody Shop shop) {
        try {
            // 判断id是为空
            if (shop.getId() == null) {
                shopService.add(shop); // 如果id为空则调用add方法
            } else {
                shopService.update(shop);// 如果id不为空则调用update方法
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作异常，以滚出"); // 操作失败
        }

    }

    // 删除店铺
    /**
     * 接口三要素：
     * 1.接口的请求地址：http://localhost:8080/dept/1
     * 2.接口的参数：@PathVariable("id") Long id
     * 3.接口的返回值：AjaxResult
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult deleteById(@PathVariable Long id) {
        try {
            shopService.delete(id); // 调用删除方法
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作异常，以滚出"); // 操作失败
        }
    }

    // 查询一个店铺
    @GetMapping("/{id}")
    public Shop queryById(@PathVariable Long id) {
        return shopService.queryById(id);
    }

    // 查询所有店铺
    @GetMapping
    public List<Shop> queryAll() {
        return shopService.queryAll();
    }

    // 分页 + 高级查询
    @PostMapping
    public PageList<Shop> queryPage(@RequestBody ShopQuery query) {
        return shopService.queryPage(query);
    }

    /**
     * 批量删除
     * 前端传的为数组，用数组接收
     */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids) {
        try {
            shopService.patchDelete(ids); // 调用删除方法
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作异常，以滚出"); // 操作失败
        }
    }

    /**
     * 店铺入驻接口
     * @param shop
     * @return
     */
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return AjaxResult.me();
        } catch (BusinessException e) {//接收自定义的业务异常
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        } catch (Exception e) {//系统级别的错误
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙，稍后重试！");
        }

    }
}
